package com.tianzeculture.stagebao.activity.artist.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.LoginAndRegisterActivity;
import com.tianzeculture.stagebao.activity.artist.ArtistEditPhotoActivity;
import com.tianzeculture.stagebao.activity.home.SeeImageActivity;
import com.tianzeculture.stagebao.activity.video.ModifyVideoActivity;
import com.tianzeculture.stagebao.bean.ArtistsBean;
import com.tianzeculture.stagebao.bean.ImageBean;
import com.tianzeculture.stagebao.bean.ListpBean;
import com.tianzeculture.stagebao.view.MyGridView;
import com.tianzeculture.stagebao.view.SqImageView;

import java.util.ArrayList;
import java.util.List;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

/**
 * Created by Administrator on 2018/5/29 0029.
 */

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.RecyclerViewHolder> {
    Activity context;
    List<ListpBean> artistsBeans;
    boolean isMine;

    public PhotoAdapter(Activity context, List<ListpBean> artistsBeans) {
        this.context = context;
        this.artistsBeans = artistsBeans;
    }

    public PhotoAdapter(Activity context, List<ListpBean> artistsBeans, boolean isMine) {
        this.context = context;
        this.artistsBeans = artistsBeans;
        this.isMine = isMine;
    }


    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_artist_photo, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return artistsBeans.size();
    }


    class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle;
        TextView tvToEditPhoto;
        TextView tvContent;
        MyGridView gvPhoto;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            tvToEditPhoto = itemView.findViewById(R.id.tvToEditPhoto);
            tvContent = itemView.findViewById(R.id.tvContent);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            gvPhoto = itemView.findViewById(R.id.gvPhoto);
        }

        public void setData(final int position) {
            String string = "";
            if (isMine)
                tvToEditPhoto.setVisibility(View.VISIBLE);
            else
                tvToEditPhoto.setVisibility(View.INVISIBLE);
            final ListpBean bean = artistsBeans.get(position);
            tvTitle.setText(bean.getTitle());
            tvContent.setText(bean.getTitle1());
            tvToEditPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Intent intent = new Intent(context, ArtistEditPhotoActivity.class);
                    Intent intent = new Intent(context, ModifyVideoActivity.class);
                    intent.putExtra("ListpBean", bean);
                    intent.putExtra("isVideo", false);
//                    intent.putStringArrayListExtra("imgs", (ArrayList<String>) artistsBeans.get(position).getArtistsUrl());
                    context.startActivityForResult(intent, 5);
                }
            });
            if (bean.getPath() != null && bean.getPath().length() > 1) {
                string = bean.getPath().substring(1, bean.getPath().length());
            } else {
                string = "";
            }
            final List<String> strings = new ArrayList<>();
            if (!"".equals(string)) {
                if (string.contains(",")) {
                    String[] arr = string.split(",");
                    for (String str : arr) {
                        strings.add(str);
                    }
                } else {
                    strings.add(string);
                }
            }
            PhotoItemAdapter itemAdapter = new PhotoItemAdapter(context, strings);
            gvPhoto.setAdapter(itemAdapter);
            gvPhoto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    context.startActivity(new Intent(context, SeeImageActivity.class).putStringArrayListExtra("list", (ArrayList<String>) strings).putExtra("position", i));
                }
            });
        }
    }

    public void setList(List<ListpBean> list) {
        artistsBeans = list;
        notifyDataSetChanged();
    }
}